Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove fast-json-stringify and allows customJSONSerializer to be a function #88

Closed
wants to merge 10 commits into from
Closed

Remove fast-json-stringify and allows customJSONSerializer to be a function #88

wants to merge 10 commits into from

Conversation

hoangvvo
Copy link
Contributor

@hoangvvo hoangvvo commented Jul 19, 2020

This PR allows options.customJSONSerializer to be a function to be called with CompilationContext to returns a serializer function. It will be a breaking change.

Something like queryToJSONSchema in json.ts is not a concern of this package and should be reimplemented in userland.

import { queryToJSONSchema } from '../user-land/json';

compileQuery(schema, document, "", {
  customJSONSerializer: (context) => {
    const jsonSchema = queryToJSONSchema(context);
    return fastJson(jsonSchema);
  }
});

Why

  • fast-json-stringify is way too large if graphql-jit is ever used in browser.
  • We pin fast-json-stringify at v1 which misses out a lot of features from v2. User should get to choose what JSON serializer to use.

Fix #83

@hoangvvo hoangvvo marked this pull request as draft July 19, 2020 22:38
@hoangvvo hoangvvo changed the title Remove fast-json-stringify and allows customJSONSerializer to be a function [For discussion, do not merge] Remove fast-json-stringify and allows customJSONSerializer to be a function Jul 19, 2020
@hoangvvo
Copy link
Contributor Author

I tried to publish it as a fork. Bundle size went down from 226.9 kb -> 70.3 kb (~70%)

https://bundlephobia.com/result?p=@hoangvvo/[email protected]

compared to

https://bundlephobia.com/[email protected]

@hoangvvo hoangvvo marked this pull request as ready for review July 19, 2020 23:10
Copy link

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

README.md Outdated
@@ -110,7 +110,7 @@ Compiles the `document` AST, using an optional operationName and compiler option
so this option should only be set to true if there are strong assurances that the values are valid.
- `customSerializers` {Object as Map, default: {}} - Replace serializer functions for specific types. Can be used as a safer alternative
for overly expensive serializers
- `customJSONSerializer` {boolean, default: false} - Whether to produce also a JSON serializer function using `fast-json-stringify`. The default stringifier function is `JSON.stringify`
- `customJSONSerializer` {function, default: false} - A function to be called with [`CompilationContext`](https://github.com/zalando-incubator/graphql-jit/blob/master/src/execution.ts#L87) to produce also a JSON serializer function. The default stringifier function is `JSON.stringify`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `customJSONSerializer` {function, default: false} - A function to be called with [`CompilationContext`](https://github.com/zalando-incubator/graphql-jit/blob/master/src/execution.ts#L87) to produce also a JSON serializer function. The default stringifier function is `JSON.stringify`
- `customJSONSerializer` {function, default: undefined} - A function to be called with [`CompilationContext`](https://github.com/zalando-incubator/graphql-jit/blob/master/src/execution.ts#L87) to produce also a JSON serializer function. The default stringifier function is `JSON.stringify`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In code, the default value is false. Is this okay to write it as undefined in the doc (even though it does not matter actually)

customJSONSerializer: false,

@ruiaraujo
Copy link
Collaborator

ruiaraujo commented Jul 22, 2020

Isn't this equivalent to removing the feature fully? The main difference is that you get access to the CompilationContext. Do you find it valuable?

@hoangvvo
Copy link
Contributor Author

hoangvvo commented Jul 24, 2020

Sorry for the late response. It is helpful to me, but I'm not sure if others are depending on it (in which case let's drop this PR).

By the name customJSONSerializer, the option should allow actually "custom" JSON serializer, not specifically the built in one.

Pretty much my reasons have already been mentioned above in this PR. Whatever in json.ts also seems to be "out of the scope" of this package.

@hoangvvo hoangvvo changed the title [For discussion, do not merge] Remove fast-json-stringify and allows customJSONSerializer to be a function Remove fast-json-stringify and allows customJSONSerializer to be a function Oct 25, 2020
@ruiaraujo
Copy link
Collaborator

@boopathi what do you think about this? We could move to a mutiple package setup and add the JSON stringifier with the idea of later adding a protobuf one for example.

@boopathi
Copy link
Member

yes I think that's a good idea. we can add and move a few other things as well.

@hoangvvo hoangvvo closed this Mar 7, 2021
@hoangvvo hoangvvo deleted the make-fast-json-stringify-optional branch March 7, 2021 04:33
@hoangvvo hoangvvo restored the make-fast-json-stringify-optional branch March 7, 2021 04:43
@hoangvvo hoangvvo reopened this Mar 7, 2021
@hoangvvo
Copy link
Contributor Author

Are we still interested in proceeding in this direction? I would love to reopen another PR after discussing the good approach forward. Thanks!

@hoangvvo hoangvvo closed this May 27, 2021
@hoangvvo hoangvvo deleted the make-fast-json-stringify-optional branch June 5, 2021 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make fast-json-stringify optional
4 participants